<?php 
namespace app\common\lib;

/**
 *文件操作类
 */
class FileOperate
{   

	/**
	 * 保存数据到文件
	 * @param  string $fileName 文件路径
	 * @param  string $data       [description]
	 */
	public static function save( $path = '' , $fileName = "" , $data = "" )
    {
        	try {
                if( !file_exists( $path ) ){
    	        	//create file
    	            mkdir( $path , 0777 , true ); 

    		        //设置权限
    		        chmod( $path , 0777 );
		        }
                $fileName = $path.'/'.$fileName;
                if( is_array( $data ) ){
                    $data = json_encode( $data , JSON_UNESCAPED_UNICODE );
                }
	            //lock file and save data
	            file_put_contents( $fileName , $data , LOCK_EX );

	        } catch (\Exception $e) {
	            return [ 'status' => false , 'message' => '异常:'.$e->getMessage() ] ;
	    	}
        	return true;
    }


    /**
     * 获取数据
     *  @param  $fileName       文件路径
     */
    public static function get( $fileName = "" )
    {
        if( file_exists( $fileName ) ){
        	/*$arr = phpinfo( $fileName );
        	$extension = $arr [ 'extension' ] ;*/
            $exts = explode( '.', $fileName );
            $end = end($exts);
            if( $end == 'php' ){
                $data = require( $fileName );
            }else{
                $data = file_get_contents( $fileName );
            }
            return $data;
        }else{
            return  "";
        }
    }




    /**
     * 远程下载
     * @param  $url 请求连接
     * @param  $path 本地地址 例 C:/path
     */
    public function fileDownload( $url = '' , $path = '' )
    {
        $res = [ 'status' => true , 'message' => '下载完成' ];
        $arr = parse_url( $url ); 
        $fileName = basename( $arr['path'] );

        header("Content-type: text/html; charset=utf-8");

        try {
            $file = file_get_contents( $url );
        } catch ( \Exception $e ) {

            return [ 'status' => false , 'message' => '文件下载错误！'.$e->getMessage() , 'code' => '101'];

        }
        file_put_contents( $path.$fileName , $file , LOCK_EX);

        return ['status'=>true,'message'=>'上传成功！','data'=>$path.$fileName,'code' => 200];
 
    }






	
}